<?php
/**
 * Контроллер гостевых книг административной панели
 * 
 * @category Phorm
 * @package Controller_Module_Control
 * @name /Phorm/Controller/Module/Control/Gbooks.php
 * @copyright phorm.ru
 * @since 05.05.2012
 * @author vladimir@kachalov.net
 */

class Phorm_Controller_Module_Control_Gbooks extends Phorm_Controller_Controller {
	
	/**
	 * Экземпляр класса материалов сайта типа Гостевые книги
	 * @var Phorm_Resource_Gbooks
	 */
	public $Gbooks;
	
	
	/**
	 * Предиспетчер
	 * 
	 * @return void
	 */
		
	public function preDispatch() {
		
		$this->Gbooks = new Phorm_Resource_Gbooks();
		
	}
	
	/**
	 * Список гостевых книг
	 * 
	 * @return void
	 */
		
	public function indexAction() {
		
		$options = $this->Gbooks->getGridOptions('index');
		
		if($this->_request->isXmlHttpRequest()) {
			
			$rows = $this->Gbooks->getGbooksList($this->_params);
			$paginator = $this->Gbooks->getPaginator();
			$this->_helper->json(Phorm_JQuery_DataGrid::data($rows,$paginator,$options+$this->_params));
			
		} else {
			
			$this->view->assign('ResourceDataTable',$this->view->DataGrid('ResourceDataTable',$options));
			
		}
						
	}
	
	
	/**
	 * Создание гостевой книги
	 * 
	 * @return void
	 */
	
	public function addAction() {
		
		if($this->Gbooks->isAllowedAction('add')) {
				
			$form = $this->Gbooks->getResourceForm('add');
			$form->setDefaults($this->Gbooks->getHumanValues());
				
			if($this->_request->isPost()) {
					
				if($form->isValid($this->_request->getPost()) && $this->Gbooks->saveResourceForm($form,null,'saveGbookInfo')) {
					if(isset($this->_params['categoryid'])) {
						$redirect = '/control/categories/index/categoryid/'.$this->_params['categoryid'].'/';
					} else {
						$redirect = '/control/resource/index/moduleid/'.$this->Forms->getModuleid().'/';
					}
					return $this->composeSuccessMessage('Гостевая книга сохранена',$redirect);	
				} else {
					$this->composeTransactionErrorMessage($this->Gbooks->_errors);	
				}
			}
			
			$this->view->assign('Form',$form);
				
		} else {
				
			return $this->composeErrorMessage('Недостаточно прав для создания материала');
				
		}
		
	}
	
	
	/**
	 * Редактирование гостевой книги
	 * 
	 * @return void
	 */
	
	public function editAction() {
		
		if(isset($this->_params['resourceid']) && $GbookInfo = $this->Gbooks->getFullGbookInfo($this->_params['resourceid'])) {
			
			if($this->Gbooks->isAllowedAction('edit',$GbookInfo['categories'],$GbookInfo['creator'])) {
				
				$form = $this->Gbooks->getResourceForm('edit');
				$form->setDefaults($GbookInfo);
				
				if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
						
					if($this->Gbooks->saveResourceForm($form,$GbookInfo['resourceid'],'saveGbookInfo')) {
						if(isset($this->_params['categoryid'])) {
							$redirect = '/control/categories/index/categoryid/'.$this->_params['categoryid'].'/';
						} else {
							$redirect = '/control/resource/index/moduleid/'.$this->Forms->getModuleid().'/';
						}
						return $this->composeSuccessMessage('Изменения внесены',$redirect);
					} else {
						$this->composeTransactionErrorMessage($this->Gbooks->_errors);
					}
					
				}
				
				$this->view->assign('Form',$form);
				
			} else {
				
				return $this->composeErrorMessage('Недостаточно прав для внесения изменений');
				
			}
			
		} else {
			
			return $this->composeErrorMessage('Гостевая книга с указанным Id не найдена');
			
		}
		
	}
	
	
	/**
	 * Список записей гостевой книги
	 * 
	 * @return void
	 */
	
	public function entriesAction() {
		
		if(isset($this->_params['resourceid']) && $GbookInfo = $this->Gbooks->getFullGbookInfo($this->_params['resourceid'])) {
			
			$this->view->assign('Header',$this->_translate->_('Список записей в гостевой книге') . ' &laquo;' . $GbookInfo['resourcename'] . '&raquo;');
			
			$options = Phorm_Config::xml('/resources/gbooks/control/grids.xml', 'entries');
		
			if($this->_request->isXmlHttpRequest()) {
				
				$rows = $this->Gbooks->getEntriesList($GbookInfo['resourceid']);
				$paginator = $this->Gbooks->getPaginator();
				$this->_helper->json(Phorm_JQuery_DataGrid::data($rows,$paginator,$options));
				
			} else {
				
				$this->view->assign('EntriesDataTable',$this->view->DataGrid('EntriesDataTable',$options));
				
			}
			
		} else {
			
			return $this->composeErrorMessage('Гостевая книга с указанным Id не найдена');
			
		}
		
	}
	
	
	/**
	 * Просмотр записи в гостевой книге
	 * 
	 * @return void
	 */
	
	public function viewAction() {
		
		if(isset($this->_params['entryid']) && $EntryInfo = $this->Gbooks->getEntryInfo($this->_params['entryid'])) {
			
			$this->view->assign("Header",$this->_translate->_('Просмотр записи гостевой книги') . ' &laquo;' . $EntryInfo['resourcename'] . '&raquo;');
			$this->view->assign('EntryInfo',$EntryInfo);
			$this->Forms->updateEntryStatus($EntryInfo['entryid']); // Делаем статус прочитанным
			
		} else {
			
			return $this->composeErrorMessage('Запись с указанным Id не найдена');
			
		}
		
	}
	
}